#include <iostream>
#include "LinkedList.h"

using namespace std;

// 97.48 %
class Solution
{
  public:
	ListNode *swapPairs(ListNode *head)
	{
		ListNode *dummyHead = new ListNode(0);
		dummyHead->next = head;

		ListNode *p = dummyHead;
		while (p->next && p->next->next)
		{
			ListNode *node1 = p->next;
			ListNode *node2 = node1->next;
			ListNode *pnext = node2->next;

			node2->next = node1;
			node1->next = pnext;
			p->next = node2;
			p = node1;
		}
		ListNode *retNode = dummyHead->next;
		delete dummyHead;
		return retNode;
	}
};

int main(int argc, char const *argv[])
{
	int arr[] = {1, 2, 3, 4};
	int n = sizeof(arr) / sizeof(arr[0]);
	ListNode *list = createLinkedList(arr, n);
	printLinkedList(list);
	list = Solution().swapPairs(list);
	printLinkedList(list);
	deleteLinkedList(list);
	return 0;
}
